package com.pi.shisan.service.util;

import com.pi.shisan.service.annotation.CalcNextTimeType;
import com.pi.shisan.service.constants.ClockTypeEnum;
import com.pi.shisan.service.service.ClockNextTimeService;

import java.util.HashMap;
import java.util.Map;

public class CalcNextTimeRegister {

    private final static Map<ClockTypeEnum, String> MAPPING = new HashMap<>();

    private static boolean finishedRegister = false;

    public static void register(CalcNextTimeType type, String serviceName) {
        if (finishedRegister) {
            throw new RuntimeException("已完成注册，不可再注册");
        }
        String exists = MAPPING.put(type.value(), serviceName);
        if (exists != null) {
            throw new RuntimeException("注册类型重复了");
        }
    }

    public static ClockNextTimeService getFromMapping(ClockTypeEnum type) {
        return (ClockNextTimeService) AppUtil.getApplicationContext().getBean(MAPPING.get(type));
    }

    public static void finishRegister() {
        finishedRegister = true;
    }

}
